package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.adapter.CategoryChildAdapter;
import com.future.mayi.adapter.CategoryParentAdapter;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.ActivityDetailResponse;
import com.future.mayi.response.SortGoodsChildResponse;
import com.future.mayi.response.SortGoodsResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class SortFragment extends BaseFragment implements OnItemClickListener, XRecyclerView.LoadingListener {

    @BindView(R.id.shop_category_parent_lv)
    XRecyclerView lvCategoryParent;
    @BindView(R.id.shop_category_child_lv)
    XRecyclerView lvCategoryChild;

    CategoryParentAdapter parentAdapter;
    CategoryChildAdapter childAdapter;

    private int page = 0;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_sort;
    }

    @Override
    protected void initView() {
        parentAdapter = new CategoryParentAdapter(getActivity(), lvCategoryParent);
        lvCategoryParent.setHasFixedSize(true);
        lvCategoryParent.setPullRefreshEnabled(false);
        lvCategoryParent.setLoadingMoreEnabled(false);
        lvCategoryParent.setLayoutManager(new LinearLayoutManager(mContext));
        lvCategoryParent.setItemAnimator(new DefaultItemAnimator());
        lvCategoryParent.setAdapter(parentAdapter);

        childAdapter = new CategoryChildAdapter(getActivity(), lvCategoryParent);
        lvCategoryChild.setHasFixedSize(true);
        lvCategoryChild.setLayoutManager(new GridLayoutManager(mContext, 4));
        lvCategoryChild.setItemAnimator(new DefaultItemAnimator());
        lvCategoryChild.setAdapter(childAdapter);

    }

    @Override
    protected void initListener() {
        parentAdapter.setOnItemClickListener(this);
        lvCategoryChild.setLoadingListener(this);
    }

    @Override
    protected void initData() {
        initSortParent();
    }

    public void showFragment() {

    }


    public void refresh() {

    }

    @Override
    public void OnItem(View view, int position) {
        parentAdapter.setCurPosition(position);
        page = 0;
        getSortChild(parentAdapter.getCurId());
    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    private void initSortParent() {
        new HttpRequest<ActivityDetailResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.goodsCatList)
                .setListener(new HttpRequest.OnNetworkListener<SortGoodsResponse>() {
                    @Override
                    public void onSuccess(SortGoodsResponse response) {
                        if (response.detailModel != null) {
                            parentAdapter.addMore(response.detailModel.getList_level1());
                            getSortChild(response.detailModel.getList_level1().get(0).getCate_id());
                        }
                    }


                    @Override
                    public void onFail(String message, SortGoodsResponse response) {
                    }
                }).start(new SortGoodsResponse());
    }


    private void getSortChild(String cateId) {
        new HttpRequest<ActivityDetailResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.goodsListByCatId)
                .addParam("cate_id", cateId)
                .addParam("p", page)
                .addParam("psize", 20)
                .setListener(new HttpRequest.OnNetworkListener<SortGoodsChildResponse>() {
                    @Override
                    public void onSuccess(SortGoodsChildResponse response) {
//                        if (page == 0 && (response.list == null || response.list.size() == 0)) {
                            HCUtils.refreshListForPage(lvCategoryChild, childAdapter, response.list, page, 10);
//                        }
                    }

                    @Override
                    public void onFail(String message, SortGoodsChildResponse response) {
                        page = HCUtils.refreshFail(lvCategoryChild, page, getActivity().getApplicationContext(), message);

                    }
                }).start(new SortGoodsChildResponse());
    }


    @Override
    public void onRefresh() {
        page = 0;
        getSortChild(parentAdapter.getCurId());
    }

    @Override
    public void onLoadMore() {
        page++;
        getSortChild(parentAdapter.getCurId());
    }
}
